App.controller('mediaLineController', function($rootScope, $scope, $state, Page, $interval, $timeout, XCode) {
	Page.ajaxPostQuiet('anon/markType/tree.do', null, function (resp) {
        $scope.marks = resp.data;
    });
	   
    $scope.lineGrid = {
		column_defs: [
			{
				target: 5,
				buttons: [
							{
								name: '编辑',
								clazz: 'btn-edit',
								icon: 'fa-edit',
								event: function(row){
									row.mediaMarks = [];
									Page.ajaxGetQuiet('media/line/' + row.lineId + '/marks.do', null, function (resp) {
								         $(resp.data).each(function(index, mark) {
								        	 row.mediaMarks[index] = mark.markId;
								         });
								         $scope.lineEditModal.init(row).show();
								    });
								}
							}
				]
			}
        ],
		syncLines : function(){
			Page.ajaxPost('media/line/refresh.do', null, function(resp) {
        		Page.notice('线路数据刷新完成!');
        		$scope.lineGrid.query();
    		});
		}
    };
    
    // 线路信息
    $scope.lineEditModal = { 
    	init : function(line){
    		this.mediaLine = angular.copy(line);
    		return this;
    	},
		onShow : function(){
			this.setTitle('线路信息编辑【' + this.mediaLine.lineName  + '】');
		   	var $select = $('#mediaLineMark_multiSelect');
	       	$select.multiSelect({
	           selectableOptgroup: true,
	           selectableHeader : '请选择标签：',
	           selectionHeader: '已选择标签：',
	           cssClass:'tags_multiselect'
	       	});
	       	$select.data('multiselect').deselect_all();
	       	if(this.mediaLine && this.mediaLine.mediaMarks
	    		   && jQuery.isArray(this.mediaLine.mediaMarks)
	    		   && this.mediaLine.mediaMarks.length > 0){
	    	   $select.data('multiselect').select(this.mediaLine.mediaMarks,'init');
	       	}
   		},
    	onOkClick : function(){
    		$scope.lineEditForm.doValidate();
	       	if($scope.lineEditForm.$valid) {
				Page.ajaxPost('media/line/update.do', this.mediaLine, function (resp) {
				    Page.notice("线路信息保存成功!");
				    $scope.lineEditModal.hide();
				    $scope.lineGrid.query();
				});
	        }
    	},
    	onHidden : function() {
    		$scope.lineEditForm.reset();
        	this.busStop = {};
    	}
    }
    
});